package com.morpheus.redis.redisson.api.lock.single;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.morpheus.redis.redisson.api.lock.SingleLock;

public class SingleDistributedLock implements SingleLock {
	private static final Logger LOGGER = LoggerFactory.getLogger(SingleDistributedLock.class);

	private RedissonClient redissonClient = null;
	private String lockName = null;
	private RLock rlock = null;

	public SingleDistributedLock(RedissonClient redissonClient, String lockName) {
		this.redissonClient = redissonClient;
		this.lockName = lockName;
	}

	@Override
	public boolean hasLock() {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			boolean flag = this.rlock.isHeldByCurrentThread();
			LOGGER.debug("SingleDistributedLock.hasLock() flag={}", flag);
			return flag;
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.hasLock() Throwable", th);
		}
		return false;
	}

	@Override
	public void lock() {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			this.rlock.lock();
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.lock() throwable", th);
			throw new IllegalStateException(th);
		}
	}

	@Override
	public void lock(long time, TimeUnit unit) throws InterruptedException {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			this.rlock.lock(time, unit);
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.lock(time, unit) throwable", th);
			throw new IllegalStateException(th);
		}
	}

	@Override
	public void lockInterruptibly(String lockName) throws InterruptedException {
		if (this.rlock == null) {
			this.rlock = this.redissonClient.getLock(this.lockName);
		}
		this.rlock.lockInterruptibly();
	}

	@Override
	public Condition newCondition() {
		Condition condition = this.rlock.newCondition();
		return condition;
	}

	@Override
	public boolean tryLock() {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			return true;
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.tryLock() throwable", th);
		} finally {
		}
		return false;
	}

	@Override
	public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			this.rlock.tryLock(time, unit);
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.tryLock(time, unit) throwable", th);
		} finally {
		}
		return false;
	}

	@Override
	public void unlock() {
		try {
			if (this.rlock == null) {
				this.rlock = this.redissonClient.getLock(this.lockName);
			}
			this.rlock.unlock();
		} catch (Throwable th) {
			LOGGER.error("SingleDistributedLock.unlock() throwable", th);
			throw new IllegalStateException(th);
		} finally {
			this.destory();
		}
	}

	@Override
	public void destory() {
		this.rlock.forceUnlock();
		this.rlock = null;
	}
}
